package com.etoak.service.impl;

import com.etoak.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean like(String key, String username) {
        Boolean exits = stringRedisTemplate.opsForHash().hasKey(key, username);
        if(exits) {
            stringRedisTemplate.opsForHash().delete(key,username);
        } else {
            stringRedisTemplate.opsForHash().put(key,username,"1");
        }
        return !exits;
    }

    @Override
    public long getCount(String key) {
        return stringRedisTemplate.opsForHash().size(key);
    }
}
